package com.letoken.platform.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.admin.service.AdminConfigService;
import com.letoken.platform.admin.service.AnnouncementService;
import com.letoken.platform.pub.constant.AdminArticleConstant;
import com.letoken.platform.pub.constant.RedisKeyConstant;
import com.letoken.platform.pub.enums.LetokenApiResponseEnum;
import com.letoken.platform.pub.enums.NetTypeEnum;
import com.letoken.platform.pub.exception.ApiServiceException;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.pub.util.BeanUtil;
import com.letoken.platform.repo.admin.dao.*;
import com.letoken.platform.repo.admin.manager.*;
import com.letoken.platform.repo.admin.po.*;
import com.letoken.platform.repo.admin.req.config.*;
import com.letoken.platform.repo.admin.res.config.*;
import com.letoken.platform.repo.admin.res.user.QueryRoleTypeRes;
import com.letoken.platform.repo.app.dao.AppEcoLevelMapper;
import com.letoken.platform.repo.app.dao.AppUserGuideMapper;
import com.letoken.platform.repo.app.dao.BlockchainMapper;
import com.letoken.platform.repo.app.manager.AdminRechargeConfigManager;
import com.letoken.platform.repo.app.manager.TokenManager;
import com.letoken.platform.repo.app.po.AppUserGuide;
import com.letoken.platform.repo.app.po.Blockchain;
import com.letoken.platform.repo.app.res.config.QuerySwapConfigRes;
import com.letoken.platform.repo.common.CommDropDownRes;
import com.letoken.platform.repo.repo.dao.AppChainNodeMapper;
import com.letoken.platform.repo.repo.po.AppChainNode;
import com.letoken.platform.repo.utlis.PageDataUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author letokenlabs
 * @date 2022/11/24$
 */
@Service
@Log4j2
public class AdminConfigServiceImpl implements AdminConfigService {


    @Autowired
    private TokenManager tokenManager;

    @Autowired
    private AdminRechargeConfigMapper adminRechargeConfigMapper;

    @Autowired
    private AppEcoLevelMapper appEcoLevelMapper;

    @Autowired
    private AdminWithdrawConfigMapper withdrawConfigMapper;

    @Autowired
    private AdminWithdrawConfigManager adminWithdrawConfigManager;

    @Autowired
    private AdminSwapConfigMapper swapConfigMapper;

    @Autowired
    private AdminIssueConfigMapper issueConfigMapper;

    @Autowired
    private AnnouncementMapper announcementMapper;

    @Autowired
    private AnnouncementManager announcementManager;

    @Autowired
    private AppVersionMapper appVersionMapper;

    @Autowired
    private AppVersionManager appVersionManager;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Autowired
    private AppUserGuideMapper guideMapper;

    @Autowired
    private BlockchainMapper blockchainMapper;

    @Autowired
    private AdminBlockchainManager blockchainManager;

    @Autowired
    private AppChainNodeMapper appChainNodeMapper;

    @Autowired
    private AdminRechargeConfigMapper rechargeMapper;

    @Autowired
    private AdminRechargeConfigManager rechargeManager;

    @Autowired
    private AnnouncementService announcementService;


    /**
     * 新增充值配置
     *
     * @param req
     */
    @Override
    public void addRechargeConfig(AddRechargeConfigReq req) {
//        checkRechargeConfig(req.getExtractRatio(), req.getProceduresRatio());
        AdminRechargeConfig flag = adminRechargeConfigMapper.queruConfigByTokenId(req.getTokenId());
        if (ObjectUtil.isNotEmpty(flag)) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_APP_EXCHANGE_004);
        }
        AdminRechargeConfig add = new AdminRechargeConfig();
        BeanUtil.quickCopy(req, add);
        add.setCreateTime(LocalDateTime.now());
        adminRechargeConfigMapper.insert(add);
    }


    /**
     * 查询充值配置
     *
     * @param req
     * @return
     */
    @Override
    public PageData<QueryRechargeConfigRes> queryRechargeConfigList(QueryRechargeConfigReq req) {
        Page<QueryRechargeConfigRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<QueryRechargeConfigRes> res = adminRechargeConfigMapper.queryRechargeConfigList(page, req.getTokenId());
        return PageDataUtil.getPageData(res);
    }


    /**
     * 修改充值配置
     *
     * @param req
     */
    @Override
    public void updateRechargeConfig(UpdateRechargeConfigReq req) {
//        checkRechargeConfig(req.getExtractRatio(), req.getProceduresRatio());
        AdminRechargeConfig flag = rechargeManager.queryRechargeConfig(req.getTokenId());
        if (ObjectUtil.isEmpty(flag)) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_APP_EXCHANGE_005);
        }
        if (!req.getTokenId().equals(flag.getTokenId())) {
            throw new ApiServiceException("修改配置时不能修改代币类型!");
        }
        BeanUtil.quickCopy(req, flag);
        flag.setUpdateTime(LocalDateTime.now());
        rechargeManager.updateRecharge(flag.getTokenId(), flag);

    }


    /**
     * 校验提现钱包比例和手续费钱包比例
     *
     * @param extractRatio
     * @param proceduresRatio
     */
    private void checkRechargeConfig(Integer extractRatio, Integer proceduresRatio) {
        if (extractRatio < 1 || extractRatio > 99) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_ADMIN_CONFIG_001);
        }
        if (proceduresRatio < 1 || proceduresRatio > 99) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_ADMIN_CONFIG_002);
        }
        if (extractRatio + proceduresRatio != 100) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_ADMIN_CONFIG_006);
        }
    }


    /**
     * 添加提现配置
     *
     * @param req
     */
    @Override
    public void addExtractConfig(AddExtractConfigReq req) {
        if (req.getFeeAmount().scale() > 2) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_ADMIN_CONFIG_005);
        }
        AdminWithdrawConfig flag = adminWithdrawConfigManager.queryByToken(req.getTokenId());
        if (ObjectUtil.isNotEmpty(flag)) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_APP_EXCHANGE_011);
        }
        AdminWithdrawConfig add = new AdminWithdrawConfig();
        BeanUtil.quickCopy(req, add);
        add.setCreateTime(LocalDateTime.now());
        add.setUpdateTime(add.getCreateTime());
        withdrawConfigMapper.insert(add);
    }


    /**
     * 修改提现配置
     *
     * @param req
     */
    @Override
    public void updateExtractConfig(UpdateExtractConfigReq req) {
        //Gas费最小0，允许输入2位小数(0.11)
        if (req.getFeeAmount().scale() > 2) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_ADMIN_CONFIG_005);
        }
        AdminWithdrawConfig flag = adminWithdrawConfigManager.queryByToken(req.getTokenId());
        if (ObjectUtil.isEmpty(flag)) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_APP_EXCHANGE_005);
        }
        if (!req.getTokenId().equals(flag.getTokenId())) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_APP_EXCHANGE_012);
        }
        AdminWithdrawConfig update = new AdminWithdrawConfig();
        BeanUtil.quickCopy(req, update);
        update.setTokenId(null);
        update.setUpdateTime(LocalDateTime.now());
        withdrawConfigMapper.updateById(update);
    }


    /**
     * 逻辑删除提现配置
     *
     * @param req
     */
    @Override
    public void deleteExtractConfig(DeleteExtractConfigReq req) {
        withdrawConfigMapper.deleteWithdraw(req);
    }


    /**
     * 查询提现配置列表
     *
     * @param req
     * @return
     */
    @Override
    public PageData<QueryExtractConfigRes> queryExtractConfigList(QueryExtractConfigReq req) {
        Page<QueryExtractConfigRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<QueryExtractConfigRes> res = withdrawConfigMapper.queryExtractConfigList(page, req.getTokenId());
        return PageDataUtil.getPageData(res);
    }


    /**
     * 查询充值配置代币下拉框列表
     *
     * @return
     */
    @Override
    public List<QueryRechargeTokenListRes> queryRechargeTokenList() {
        List<QueryRechargeTokenListRes> res = tokenManager.queryTokenList();
        return res;
    }

    /**
     * 生态建设等级
     *
     * @return
     */
    @Override
    public List<QueryLevelConfigRes> queryLevelList() {
        List<QueryLevelConfigRes> res = appEcoLevelMapper.queryLevelList();
        return res;
    }

    /**
     * 查询闪兑配置
     *
     * @param req
     * @return
     */
    @Override
    public PageData<QueryQuickswapConfigRes> queryQuickswapList(QueryQuickswapConfigReq req) {
        Page<QueryQuickswapConfigRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<QueryQuickswapConfigRes> res = swapConfigMapper.queryQuickswapList(page, req.getTokenId());
        return PageDataUtil.getPageData(res);
    }

    /**
     * 添加闪兑配置
     *
     * @param req
     */
    @Override
    public void addQuickswapConfig(AddQuickswapConfigReq req) {
        if (req.getPrice().compareTo(BigDecimal.valueOf(1.0)) < 0 || req.getPrice().compareTo(BigDecimal.valueOf(2.0)) > 0 || req.getPrice().scale() > 2) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_ADMIN_CONFIG_007);
        }
        QuerySwapConfigRes querySwapConfigRes = swapConfigMapper.querySwapConfigByTokenId(req.getTokenId());
        if (ObjectUtil.isNull(querySwapConfigRes)) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_ADMIN_CONFIG_009);
        }
        AdminSwapConfig swapConfig = new AdminSwapConfig();
        BeanUtil.quickCopy(req, swapConfig);
        swapConfig.setCreateTime(LocalDateTime.now());
        swapConfigMapper.insert(swapConfig);
    }

    /**
     * 修改闪兑配置
     *
     * @param req
     */
    @Override
    public void updateQuickswapConfig(UpdateQuickswapConfigReq req) {
        if (req.getPrice().compareTo(BigDecimal.valueOf(1.0)) < 0 || req.getPrice().compareTo(BigDecimal.valueOf(2.0)) > 0 || req.getPrice().scale() > 2) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_ADMIN_CONFIG_007);
        }
        AdminSwapConfig swapConfig = new AdminSwapConfig();
        BeanUtil.quickCopy(req, swapConfig);
        swapConfig.setUpdateTime(LocalDateTime.now());
        swapConfigMapper.updateById(swapConfig);
    }

    /**
     * 逻辑删除闪兑配置
     *
     * @param req
     */
    @Override
    public void deleteQuickswapConfig(DeleteQuickswapConfigReq req) {
        swapConfigMapper.deleteQuickswapConfig(req);
    }

    /**
     * 查询铸币配置
     *
     * @param req
     * @return
     */
    @Override
    public QueryIssueConfigRes queryIssueList(QueryIssueConfigReq req) {
        QueryIssueConfigRes res = issueConfigMapper.queryIssueList(req);
        return res;
    }

    /**
     * 修改铸币配置
     *
     * @param req
     */
    @CacheEvict(cacheNames = RedisKeyConstant.ISSUE_CONFIG, key = "'issueConfig'")
    @Override
    public void updateIssueConfig(UpdateIssueConfigReq req) {
        AdminIssueConfig issueConfig = new AdminIssueConfig();
        BeanUtil.quickCopy(req, issueConfig);
        issueConfig.setUpdateTime(LocalDateTime.now());
        issueConfigMapper.updateById(issueConfig);
    }

    /**
     * 查询流通配置
     *
     * @param req
     * @return
     */
    @Override
    public QueryCirculationConfigRes queryCirculationList(QueryCirculationConfigReq req) {
        QueryCirculationConfigRes res = issueConfigMapper.queryCirculationList(req);
        return res;
    }

    /**
     * 修改流通配置
     *
     * @param req
     */
    @CacheEvict(cacheNames = RedisKeyConstant.ISSUE_CONFIG, key = "'issueConfig'")
    @Override
    public void updateCirculationConfig(UpdateCirculationConfigReq req) {
        AdminIssueConfig adminIssueConfig = new AdminIssueConfig();
        adminIssueConfig.setId(req.getId());
        adminIssueConfig.setCirculateRate(req.getCirculateRate());
        issueConfigMapper.updateById(adminIssueConfig);
    }

    /**
     * 查询奖励配置
     *
     * @param req
     * @return
     */
    @Override
    public PageData<QueryRewardConfigRes> queryRewardList(QueryRewardConfigReq req) {
        Page<QueryRewardConfigRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<QueryRewardConfigRes> res = issueConfigMapper.queryRewardList(page, req);
        return PageDataUtil.getPageData(res);
    }


    /**
     * 修改奖励配置
     *
     * @param req
     */
    @Override
    public void updateRewardConfig(UpdateRewardConfigReq req) {
        AdminIssueConfig adminIssueConfig = new AdminIssueConfig();
        adminIssueConfig.setId(req.getId());
        adminIssueConfig.setShareAccelerateRound(req.getShareAccelerateRound());
        issueConfigMapper.updateById(adminIssueConfig);
    }

    /**
     * 查询版本更新
     *
     * @param req
     * @return
     */
    @Override
    public PageData<QueryVersionRes> queryVersionList(QueryVersionReq req) {
        Page<QueryVersionRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<QueryVersionRes> res = appVersionMapper.queryVersionList(page, req);
        return PageDataUtil.getPageData(res);
    }

    /**
     * 修改版本更新
     *
     * @param req
     * @return
     */
    @CacheEvict(cacheNames = RedisKeyConstant.VERSION_CHECK, allEntries = true)
    @Override
    public void updateVersionConfig(UpdateVersionReq req) {
        AppVersion appVersion = new AppVersion();
        BeanUtil.quickCopy(req, appVersion);
        appVersion.setCreateTime(LocalDateTime.now());
        appVersionMapper.updateById(appVersion);
    }

    /**
     * 新增版本更新
     *
     * @param req
     * @return
     */
    @CacheEvict(cacheNames = RedisKeyConstant.VERSION_CHECK, allEntries = true)
    @Override
    public void addVersionConfig(AddVersionReq req) {
        //判断版本是否重复
        boolean flag = appVersionManager.versionIsRepeat(req.getClient(), req.getCurrentVersion());
        if (flag) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_ADMIN_USER_032);
        }
        AppVersion appVersion = new AppVersion();
        BeanUtil.quickCopy(req, appVersion);
        appVersion.setCreateTime(LocalDateTime.now());
        appVersionMapper.insert(appVersion);
    }

    /**
     * 删除版本更新
     *
     * @param req
     * @return
     */
    @CacheEvict(cacheNames = RedisKeyConstant.VERSION_CHECK, allEntries = true)
    @Override
    public void deleteVersionConfig(DeleteVersionReq req) {
        appVersionMapper.deleteById(req.getId());
    }


    /**
     * 查询公告配置
     *
     * @param req
     * @return
     */
    @Override
    public PageData<QueryAnnouncementRes> queryAnnouncementList(QueryAnnouncementReq req) {
        Page<QueryAnnouncementRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<QueryAnnouncementRes> res = announcementMapper.queryAnnouncementList(page, req);
        return PageDataUtil.getPageData(res);
    }

    @Override
    public PageData<QueryAnnouncementRes> queryOneLevelAnnouncementList(QueryAnnouncementReq req) {
        Page<QueryAnnouncementRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<QueryAnnouncementRes> res = announcementMapper.queryOneLevelAnnouncementList(page, req);
        return PageDataUtil.getPageData(res);
    }

    @Override
    public List<QueryAnnouncementRes> queryTwoLevelAnnouncementList(Long pid) {
        return announcementMapper.queryTwoLevelAnnouncementList(pid);
    }

    /**
     * 新增公告配置
     *
     * @param req
     */
    @CacheEvict(cacheNames = RedisKeyConstant.ANNOUNCEMENT_LIST, allEntries = true)
    @Override
    public void addAnnouncementConfig(AddAnnouncementReq req) {
        Announcement announcement = new Announcement();
        BeanUtil.quickCopy(req, announcement);
        Long pid = announcement.getPid();
        LocalDateTime publishTime = req.getPublishTime();
        if (pid == null) {
            pid = 0L;
            announcement.setPid(pid);
        }
        if (pid == 0L && publishTime == null) {
            throw new ApiServiceException("开始时间不能为空！");
        }
        if (pid != 0) {
            LambdaQueryWrapper<Announcement> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(Announcement::getId, pid);
            Announcement announcementParent = announcementMapper.selectOne(queryWrapper);
            if (announcementParent == null) {
                throw new ApiServiceException("父公告配置不存在！");
            }
            if (publishTime == null) {
                announcement.setPublishTime(announcementParent.getPublishTime());
            }
            if (req.getExpireTime() == null) {
                announcement.setExpireTime(announcementParent.getExpireTime());
            }
            if (StringUtils.isBlank(announcement.getTitle())) {
                announcement.setTitle(announcementParent.getTitle());
            }
            if (StringUtils.isBlank(announcement.getContent())) {
                announcement.setContent(announcementParent.getContent());
            }
        }

        boolean flag = announcementManager.queryCountByPidAndLang(announcement.getPid(), announcement.getLang());
        if (flag) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_ADMIN_CONFIG_008);
        }
        announcement.setCreateTime(LocalDateTime.now());
        announcement.setUpdateTime(LocalDateTime.now());
        announcement.setIsShow(true);
        announcementMapper.insert(announcement);
    }

    /**
     * 修改公告配置
     *
     * @param req
     */
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = RedisKeyConstant.ANNOUNCEMENT_LIST, allEntries = true),
                    @CacheEvict(cacheNames = RedisKeyConstant.ANNOUNCEMENT_DETAIL, key = "'detail_' + #req.id")
            }
    )
    @Override
    public void updateAnnouncementConfig(UpdateAnnouncementReq req) {
        Long id = req.getId();
        Announcement announcement = new Announcement();
        BeanUtil.quickCopy(req, announcement);
        announcement.setUpdateTime(LocalDateTime.now());
        announcementMapper.updateById(announcement);
        List<Announcement> announcementList = announcementManager.queryListByPid(id);
        if (CollUtil.isNotEmpty(announcementList)) {
            for (Announcement item : announcementList) {
                Long itemId = item.getId();
                BeanUtil.quickCopy(req, item);
                item.setId(itemId);
                item.setTitle(null);
                item.setUpdateTime(LocalDateTime.now());
            }
            announcementService.updateBatchById(announcementList);
        }
    }

    /**
     * 删除公告配置
     *
     * @param req
     */
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = RedisKeyConstant.ANNOUNCEMENT_LIST, allEntries = true),
                    @CacheEvict(cacheNames = RedisKeyConstant.ANNOUNCEMENT_DETAIL, key = "'detail_' + #req.id")
            }
    )
    @Override
    public void deleteAnnouncementConfig(DeleteAnnouncementReq req) {
        Long id = req.getId();
        List<Announcement> announcementList = announcementManager.queryListByPid(id);
        if (CollUtil.isNotEmpty(announcementList)) {
            announcementManager.delListByPid(id);
        }
        // 物理删除
        announcementMapper.deleteById(req.getId());
    }

    /**
     * 展示公告
     *
     * @param id
     */
    @CacheEvict(cacheNames = RedisKeyConstant.ANNOUNCEMENT_LIST, allEntries = true)
    @Override
    public void ShowAnnouncementConfig(Long id) {
        Announcement announcement = new Announcement();
        announcement.setId(id);
        announcement.setIsShow(true);
        announcementMapper.updateById(announcement);
        List<Announcement> announcementList = announcementManager.queryListByPid(id);
        if (CollUtil.isNotEmpty(announcementList)) {
            for (Announcement item : announcementList) {
                item.setIsShow(true);
            }
            announcementService.updateBatchById(announcementList);
        }
    }

    /**
     * 隐藏公告
     *
     * @param id
     */
    @CacheEvict(cacheNames = RedisKeyConstant.ANNOUNCEMENT_LIST, allEntries = true)
    @Override
    public void hideAnnouncementConfig(Long id) {
        Announcement announcement = new Announcement();
        announcement.setId(id);
        announcement.setIsShow(false);
        announcementMapper.updateById(announcement);
        List<Announcement> announcementList = announcementManager.queryListByPid(id);
        if (CollUtil.isNotEmpty(announcementList)) {
            for (Announcement item : announcementList) {
                item.setIsShow(false);
            }
            announcementService.updateBatchById(announcementList);
        }
    }

    /**
     * 查询用户协议
     *
     * @return
     */
    @Cacheable(cacheNames = RedisKeyConstant.AGREEMENT, key = "'agreement'", sync = true)
    @Override
    public String queryUserAgreementList() {
        LambdaQueryWrapper<Article> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Article::getName, AdminArticleConstant.USER_AGREEMENT);
        Article article = articleMapper.selectOne(wrapper);
        return article.getContent();
    }

    /**
     * 修改用户协议
     *
     * @return
     */
    @CacheEvict(cacheNames = RedisKeyConstant.AGREEMENT, key = "'agreement'")
    @Override
    public void updateUserAgreementConfig(UpdateUserAgreementReq req) {
        LambdaQueryWrapper<Article> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Article::getName, AdminArticleConstant.USER_AGREEMENT);
        Article article = articleMapper.selectOne(wrapper);
        article.setContent(req.getContent());
        articleMapper.updateById(article);
    }

    /**
     * 查询关于我们
     *
     * @return
     */
    @Cacheable(cacheNames = RedisKeyConstant.ABOUT_US, key = "'aboutUs'", sync = true)
    @Override
    public String queryAboutUsList() {
        LambdaQueryWrapper<Article> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Article::getName, AdminArticleConstant.ABOUT_US);
        Article article = articleMapper.selectOne(wrapper);
        return article.getContent();
    }

    /**
     * 修改关于我们
     */
    @CacheEvict(cacheNames = RedisKeyConstant.ABOUT_US, key = "'aboutUs'")
    @Override
    public void updateAboutUs(UupdateAboutUsReq req) {
        LambdaQueryWrapper<Article> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Article::getName, AdminArticleConstant.ABOUT_US);
        Article article = articleMapper.selectOne(wrapper);
        article.setContent(req.getContent());
        articleMapper.updateById(article);
    }

    /**
     * 查询用户指南
     */
    @Override
    public PageData<QueryGuideRes> queryGuideList(QueryGuideReq req) {
        Page<QueryGuideRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<QueryGuideRes> res = guideMapper.queryGuideList(page, req);
        return PageDataUtil.getPageData(res);
    }

    /**
     * 新增用户指南
     */
    @Override
    public void addGuideList(AddGuideReq req) {
        AppUserGuide appUserGuide = new AppUserGuide();
        BeanUtil.quickCopy(req, appUserGuide);
        appUserGuide.setIsShow(true);
        appUserGuide.setCreateTime(LocalDateTime.now());
        appUserGuide.setUpdateTime(appUserGuide.getCreateTime());
        guideMapper.insert(appUserGuide);
    }

    /**
     * 修改用户指南
     */
    @Override
    public void updateGuideList(UpdateGuideReq req) {
        AppUserGuide appUserGuide = new AppUserGuide();
        BeanUtil.quickCopy(req, guideMapper);
        appUserGuide.setUpdateTime(LocalDateTime.now());
        guideMapper.updateById(appUserGuide);
    }

    /**
     * 删除用户指南
     */
    @Override
    public void deleteGuideList(Long id) {
        // 物理删除！
        guideMapper.deleteById(id);
    }

    @Override
    public QueryRewardConfigRes queryReward() {
        QueryRewardConfigRes res = issueConfigMapper.queryReward();
        return res;
    }

    @CacheEvict(cacheNames = RedisKeyConstant.VERSION_CHECK, allEntries = true)
    @Override
    public void forceUpdateVersionById(Integer id) {
        appVersionMapper.forceUpdateVersionById(id);
    }

    @CacheEvict(cacheNames = RedisKeyConstant.VERSION_CHECK, allEntries = true)
    @Override
    public void notForceUpdateVersionById(Integer id) {
        appVersionMapper.notForceUpdateVersionById(id);
    }

    /**
     * 角色类型下拉框
     *
     * @return
     */
    @Override
    public List<QueryRoleTypeRes> queryRoleType() {
        List<QueryRoleTypeRes> res = adminRoleMapper.selectRoleTypeRes();
        return res;
    }

    /**
     * 根据id查询公告配置
     *
     * @param id
     * @return
     */
    @Override
    public QueryAnnouncementByIdRes queryAnnouncementById(Long id) {
        QueryAnnouncementByIdRes res = announcementMapper.queryAnnouncementById(id);
        res.setId(id);
        return res;
    }

    /**
     * 根据id查询版本更新
     *
     * @param id
     * @return
     */
    @Override
    public QueryVersionByIdRes queryVersionById(Integer id) {
        QueryVersionByIdRes res = appVersionMapper.queryVersionById(id);
        res.setId(id);
        return res;
    }

    /**
     * 根据id查询充值配置
     *
     * @param id
     * @return
     */
    @Override
    public QueryRechargeConfigByIdRes queryRechargeConfigById(Integer id) {
        QueryRechargeConfigByIdRes res = adminRechargeConfigMapper.queryRechargeConfigById(id);
        res.setId(id);
        return res;
    }

    @Override
    public QueryExtractConfigByIdRes queryExtractConfigById(Integer id) {
        QueryExtractConfigByIdRes res = withdrawConfigMapper.queryExtractConfigById(id);
        return null;
    }

    /**
     * 查询代币下拉框
     *
     * @return
     */
    @Override
    public List<QueryTokenListRes> queryReTokenList() {
        return tokenManager.queryReTokenList();
    }


    @Override
    public List<CommDropDownRes> queryNetTypeList() {
        List<CommDropDownRes> res = new ArrayList<>();
        for (NetTypeEnum value : NetTypeEnum.values()) {
            CommDropDownRes dropDown = new CommDropDownRes();
            dropDown.setId(value.getId());
            dropDown.setName(value.getName());
            res.add(dropDown);
        }
        return res;
    }

    @CacheEvict(cacheNames = RedisKeyConstant.CHAIN_NODE_LIST, allEntries = true)
    @Override
    public void addChainNode(AddChainNodeReq req) {
        String netType = req.getNetType();
        String name = req.getName();
        boolean flag = blockchainManager.queryByNetTypeOrName(netType, name, null);
        if (flag) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_ADMIN_CONFIG_010);
        }
        Blockchain blockchain = new Blockchain();
        BeanUtil.quickCopy(req, blockchain);
        if (blockchain.getSort() == null) {
            Integer maxSort = blockchainMapper.queryMaxSort();
            blockchain.setSort(maxSort + 1);
        }
        blockchain.setCreateTime(LocalDateTime.now());
        blockchain.insert();

    }

    @CacheEvict(cacheNames = RedisKeyConstant.CHAIN_NODE_LIST, allEntries = true)
    @Override
    public void updateChainNode(UpdateChainNodeReq req) {
        boolean flag = blockchainManager.queryByNetTypeOrName(req.getNetType(), req.getName(), req.getId());
        if (flag) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_ADMIN_CONFIG_010);
        }
        Blockchain blockchain = new Blockchain();
        BeanUtil.quickCopy(req, blockchain);
        blockchain.updateById();
    }

    @Override
    public PageData<QueryChainNodeListRes> queryChainNodeList(QueryChainNodeReq req) {
        Page<QueryChainNodeListRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<QueryChainNodeListRes> res = blockchainMapper.queryChainNodeList(page, req);
        return PageDataUtil.getPageData(res);
    }

    @Override
    public QueryChainNodeListRes queryChainNodeById(Integer id) {

        return blockchainMapper.queryChainNodeById(id);
    }

    @CacheEvict(cacheNames = RedisKeyConstant.CHAIN_NODE_LIST, allEntries = true)
    @Override
    public String chainNodeIsShow(Integer id, Boolean flag) {
        Blockchain blockchain = new Blockchain();
        blockchain.setId(id);
        if (flag) {
            blockchain.setIsUse(1);
        } else {
            blockchain.setIsUse(0);
        }
        blockchain.updateById();
        return flag ? "启用成功！" : "禁用成功！";
    }

    @CacheEvict(cacheNames = RedisKeyConstant.CHAIN_NODE_LIST, allEntries = true)
    @Override
    public void addNodeAddressNode(AddChainNodeAddressReq req) {
        AppChainNode add = new AppChainNode();
        BeanUtil.quickCopy(req, add);

        add.setCreateTime(LocalDateTime.now());
        add.setUpdateDate(add.getCreateTime());
        add.insert();
    }

    @Override
    public PageData<QueryNodeAddressListRes> queryNodeAddressList(QueryAddressNodeReq req) {
        Page<QueryNodeAddressListRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<QueryNodeAddressListRes> res = appChainNodeMapper.queryNodeAddressList(page, req);
        return PageDataUtil.getPageData(res);
    }

    @Override
    public QueryNodeAddressRes queryNodeAddressById(Integer id) {
        return appChainNodeMapper.queryNodeAddressById(id);
    }

    @CacheEvict(cacheNames = RedisKeyConstant.CHAIN_NODE_LIST, allEntries = true)
    @Override
    public void updateChainNodeAddress(UpdateNodeAddressReq req) {
        AppChainNode update = new AppChainNode();
        BeanUtil.quickCopy(req, update);

        update.setUpdateDate(LocalDateTime.now());
        update.updateById();
    }

    @CacheEvict(cacheNames = RedisKeyConstant.CHAIN_NODE_LIST, allEntries = true)
    @Override
    public String nodeAddressIsShow(Integer id, Boolean flag) {
        AppChainNode appChainNode = new AppChainNode();
        appChainNode.setId(id);
        if (flag) {
            appChainNode.setStatus(1);
        } else {
            appChainNode.setStatus(0);
        }
        appChainNode.updateById();
        return flag ? "节点地址启用成功！" : "节点地址禁用成功";
    }

    @Override
    public QueryAnnouncementByIdRes queryByPidAndLang(QueryPidAndLangReq req) {
        QueryAnnouncementByIdRes res = announcementMapper.queryByPidAndLang(req);
        return res;
    }

    @Override
    public void isLockedRechargeConfig(Integer id, Boolean flag) {
        AdminRechargeConfig rechargeConfig = rechargeMapper.selectById(id);
        rechargeConfig.setLocked(flag);
        rechargeManager.updateRecharge(rechargeConfig.getTokenId(), rechargeConfig);
    }


}
